<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
            <el-form-item label="省" prop="provinceId">
                <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" :disabled="!isAdmin" clearable>
                    <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="市" prop="cityId">
                <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                    <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="区/县" prop="countiesId">
                <el-select v-model="queryParams.countiesId" placeholder="请选择区/县" clearable>
                    <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="活动名称" prop="iaName">
                <el-input v-model="queryParams.iaName" placeholder="请输入活动名称" clearable />
            </el-form-item>
            <el-form-item label="图件分类">
                <el-form-item prop="types" class="type">
                    <el-select v-model="queryParams.types" placeholder="请选择图件分类" @change="changeType" clearable>
                        <el-option v-for="item in optionType" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <span style="display:inline-block;width:30px;text-align:center;color:#606266;">—</span>
                <el-form-item prop="tContent">
                    <el-select v-model="queryParams.tContent" placeholder="请选择评估内容" clearable>
                        <el-option v-for="item in optionContent" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form-item>
            <el-form-item label="图件名称" prop="tName">
                <el-input v-model="queryParams.tName" placeholder="请输入图件名称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" @click="handleDownloads" :disabled="multiple">
                    <svg-icon class-name="download-icon" icon-class="download" /><span
                        style="margin-left:10px">批量下载</span>
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="图件名称" align="center" prop="tname"  width="250" show-overflow-tooltip>
                <template #default="scope">
                    <span style="color: #409eff;">{{scope.row.tname}}</span>
                </template>
            </el-table-column>
            <el-table-column label="图件分类" align="center" prop="types" />
            <el-table-column label="评估内容" align="center" prop="tcontent" width="220" show-overflow-tooltip />
            <el-table-column label="活动名称" align="center" prop="iaName" width="250" show-overflow-tooltip />
            <el-table-column label="省" align="center" prop="iaProvince" show-overflow-tooltip/>
            <el-table-column label="市" align="center" prop="iaCity" show-overflow-tooltip/>
            <el-table-column label="区（县）" align="center" prop="iaCounties" show-overflow-tooltip/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
                <template #default="scope">
                    <el-button type="text" @click="handleDelate(scope.row)">查看</el-button>
                    <el-button type="text" @click="handleDownload(scope.row)">下载</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />

        <el-dialog v-model="open" width="400px" title="查看" :close-on-click-modal="false" append-to-body>
        <p class="dialogTitle">{{ form.tname }}</p>
        <image-upload ref="imageUpload" :limit="limit" v-model="form.attach" :isShowTip="false" :disabled=true />
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="cancel">关闭</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
</template>
  
<script setup name="imgQuery">
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllList, getPictureData, getPermission } from "@/api/resultsReportImg/imgQuery.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const loading = ref(false);
const total = ref(0);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const limit = ref(1);
const showSearch = ref(true);
const provinceData = ref([]);//省
const cityData = ref([]);//市
const areaData = ref([]);//区、县
const isAdmin = ref(true);
const optionType = [{ label: '地震危险性评估', value: '地震危险性评估' }, { label: '房屋地震灾害风险评估', value: '房屋地震灾害风险评估' }, { label: '次生灾害隐患评估', value: '次生灾害隐患评估' }, { label: '人员伤亡评估', value: '人员伤亡评估' }, { label: '需紧急安置人员', value: '需紧急安置人员' }, { label: '重点场所风险评估', value: '重点场所风险评估' }, { label: '辅助分析', value: '辅助分析' }]
const optionContent = ref([]);
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        provinceId: '',
        cityId: '',
        countiesId: '',
        types: '',
        tContent: '',
        iaName: '',
        tName: '',
    },
    form:{
        tname:'',
        attach:[],
    }
})
const { queryParams, form } = toRefs(data)
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    queryParams.value.page = 1;
    getPermissionData();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        dataList.value = response.data.actMaps;
        total.value = response.data.total;
        loading.value = false;
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.attachId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 改变类型
function changeType(val) {
    queryParams.value.srType='';
    if (val == '地震危险性评估') {
        optionContent.value = [{ label: '断层分布图', value: '断层分布图' }, { label: '地震构造图', value: '地震构造图' }, { label: '历史地震分布图', value: '历史地震分布图' }, { label: '地震活动趋势分析(图片)', value: '地震活动趋势分析(图片)' }, { label: '地震危险性分析', value: '地震危险性分析' }]
    } else if (val == '房屋地震灾害风险评估') {
        optionContent.value = [{ label: '房屋抗震能力评估', value: '房屋抗震能力风险初步评估分区图' }, { label: '房屋破坏评估', value: '房屋破坏分布图' }]
    } else if (val == '次生灾害隐患评估') {
        optionContent.value = [{ label: '危险源分布图', value: '危险源分布图' }, { label: '危险源避让分布图', value: '危险源避让分布图' }, { label: '地质灾害隐患点分布图', value: '地震地质灾害隐患点分布图' }]
    } else if (val == '人员伤亡评估') {
        optionContent.value = [{ label: '人口分布图', value: '人口分布图' }, { label: '人员伤亡分布图', value: '人员伤亡分布图' }, { label: '地震灾害风险等级分布图', value: '地震灾害风险等级分布图' }]
    } else if (val == '需紧急安置人员') {
        optionContent.value = [{ label: '需紧急安置人员', value: '需紧急安置人员' }]
    } else if (val == '重点场所风险评估') {
        optionContent.value = [{ label: '重点场所风险评估', value: '重点场所风险评估' }]
    } else if (val == '辅助对策分析') {
        optionContent.value = [{ label: '物资储备点分布图', value: '物资储备点分布图' }, { label: '避难场所分布图', value: '避难场所分布图' }, { label: '学校分布图', value: '学校分布图' }, { label: '医院分布图', value: '医院分布图' }, { label: '地震应急疏散辅助决策图', value: '地震应急疏散辅助决策图' }]
    } else{
        optionContent.value = []
    }
}

// 查看
function handleDelate(row){
    form.value.tname = row.tname;
    getPicture(row.attachId)
    open.value = true;
}

function getPicture(id){
    getPictureData(id).then(res=>{
        form.value.attach =[res.data];
    })
}

/** 批量下载操作 */
function handleDownloads() {
    proxy.download("/attachment/downForZip/"+ ids.value.join(","), { }, '图件.zip');
};

// 单个下载文件
function handleDownload(row) {
	var query = {
		attachId:row.attachId,
		name:row.tname
	}
	location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId="+ encodeURIComponent(query.attachId);
}

// 关闭按钮
function cancel(){
    open.value = false;
    form.value.attach=[];
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then(response => {
        provinceData.value = response.data;
        getPermissionData();
    });
}
function chooseProvince(value) {
    queryParams.value.cityId = null;
    queryParams.value.prefectureId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
}

function chooseCity(value) {
    queryParams.value.prefectureId = null;
    areaData.value = [];
    getChildArea(value).then(response => {
        areaData.value = response.data;
    });
}

function getPermissionData(){
    getPermission().then(res=>{
        isAdmin.value = res.data.isAdmin;
        // 如果是国家级人员
        if(isAdmin.value){
            queryParams.value.provinceId = '';
        }else{
            queryParams.value.provinceId = res.data.provinceId;
        }
        getList();
    });
}

/*getProvinceData();*/

/*zyt--首页跳转参数*/
const route = useRoute();
//获取首页跳转参数
function getIaName() {
  let iaName = route.query.iaName;
  if(iaName!=undefined && iaName!=null){
    queryParams.value.iaName = iaName;
    getProvinceData();
  }else{
    getProvinceData();
  }
}

getIaName();
</script>
  
<style lang="scss" scoped>
.type{
    margin-right: 0px!important;
}
</style>